﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using VersionComparer.Images;

namespace VersionComparer.DataModel.FileSystem
{
    public class FolderItem : FileItem
    {
        public FolderItem(string fullPath)
            : base(fullPath)
        {

        }


        protected override System.Drawing.Image GetImage()
        {
            return ImageResources.folder;
        }
        protected override ComparableItem[] CreateChilds()
        {
            string fullText = FullText;
            string folder = fullText ;
            string wildcard = "*.*";
            if (fullText.Contains("*"))
            {
                folder = System.IO.Path.GetDirectoryName(fullText);
                wildcard = System.IO.Path.GetFileName(fullText);
            }

            List<ComparableItem> childs = new List<ComparableItem> ();
            foreach (string childFolder in Directory.GetDirectories(folder))
                childs.Add(new FolderItem(childFolder));
            foreach (string childFile in Directory.GetFiles(folder,wildcard))
            {
                ComparableItem newItem = ComparableItemCreator.Create(new ItemId(childFile));
                
                if (newItem == null)
                    newItem = new FileItem(childFile);
                childs.Add(newItem );
                newItem.Refresh();
            }
            return childs.ToArray ();
        }
    }
}
